<?php

namespace frontend\controllers;

/**
 * @author  shuang
 * @date    2016-10-26 14:04:27
 * @version V1.0
 * @desc    
 */
use yii\web\Controller;
use Yii;
use common\components\MTools;
use frontend\models\WB_UserOrder;
use frontend\models\WB_UserPayRecord;
use common\components\GradeEvent;
use yii\helpers\Json;

class AccountController extends Controller {

    public $message = "";
    public $flag = false;

    /*
     * 充值明细
     */

    public function actionPayinfo() {
        $res = WB_UserPayRecord::getList();
        return $this->render("payinfo", $res);
    }

    /*
     * 支出明细
     */

    public function actionPayout() {
        $res = WB_UserPayRecord::getList(2);
        return $this->render("payout", $res);
    }

    /*
     * 交易明细
     */

    public function actionTradedetail() {
        $res = WB_UserPayRecord::getList();
        return $this->render("tradedetail", $res);
    }

    /*
     * 在线充值
     */

    public function actionPay() {
        if (Yii::$app->request->isPost) {
            $money = Yii::$app->request->post("txtmoney");
            if (empty($money)) {
                Yii::$app->getSession()->setFlash('error', '请输入金额');
            } else if ($money < MTools::getYiiParams("credit_amount")) {
                Yii::$app->getSession()->setFlash('error', '掌柜说：充值金额要大于' . MTools::getYiiParams("credit_amount") . '元哦(*^__^*) ……');
            } else {
                $trans = Yii::$app->db->beginTransaction();
                try {
                    //产生订单
                    $res = WB_UserOrder::createData();
                    if ($res !== false) {
                        $trans->commit();
                        $this->redirect(["payconfirm", "orderno" => $res]);
                    } else {
                        Yii::$app->getSession()->setFlash('error', '充值订单失败');
                    }
                } catch (Exception $ex) {
                    $trans->rollBack();
                    throw new \yii\web\NotFoundHttpException();
                }
            }
        }
        return $this->render("pay");
    }

    /*
     * 支付详情
     */

    public function actionPayconfirm() {
        $orderno = Yii::$app->request->get("orderno");
        $res = WB_UserOrder::find()->where("id='" . $orderno . "'")->one();
        if ($res instanceof WB_UserOrder) {
            return $this->render("payconfirm", ["res" => $res]);
        } else {
            throw new \yii\web\NotFoundHttpException();
        }
    }

    /*
     * 确认支付
     */

    public function actionPayment() {
        $order_no = Yii::$app->request->post("pay_order_no");
        $gift_name = '皓币充值';
        $pay_order_amount = Yii::$app->request->post("pay_order_amount");
        $body = '商户皓币充值';
        $show_url = "http://www.toupiao518.com";
        $alipay = new \AlipayPay();
        $alipay->return_url = 'http://www.toupiao518.com/account/paymentreturncallback.html';
        $html = $alipay->requestPay($order_no, $gift_name, $pay_order_amount, $body, $show_url);
        echo $html;
    }

    /*
     * 支付成功同步回调
     */

    public function actionPaymentreturncallback() {
        $alipay = new \AlipayPay();
        $verify_result = $alipay->verifyReturn();
        if ($verify_result) {//验证成功
            $order_no = Yii::$app->request->get("out_trade_no");
            return $this->redirect(["payconfirm", "orderno" => $order_no]);
        } else {
            echo "fail";
        }
    }

    /*
     * 支付成功异步回调
     */

    public function actionPaymentcallback() {
        $alipay = new \AlipayPay();
        $verify_result = $alipay->verifyNotify();
        if ($verify_result) {//验证成功
            $order_no = Yii::$app->request->post("out_trade_no");
            //交易状态
            $trade_status = Yii::$app->request->post('trade_status');
            if ($trade_status == 'TRADE_FINISHED' || $trade_status == 'TRADE_SUCCESS') {
                $pay_order_amount = Yii::$app->request->post("total_fee");
                $pay_subject = Yii::$app->request->post("payment_type");
                $ordermodel = WB_UserOrder::find()->where("id='" . $order_no . "'")->one();
                $event = new GradeEvent(["type" => $pay_subject, "money" => $pay_order_amount]);
                $this->on("upgrade_event", [$event, "upgrade"], ["order_no" => $order_no,"userid"=>$ordermodel->userid]);
                $this->trigger("upgrade_event", $event);
                $this->off("upgrade_event");
                if ($this->flag) {
                    return "success";
                } else {
                    //验证失败
                    return "fail";
                }
            }
        } else {
            //验证失败
            return "fail";
        }
    }

    /*
     * 提现返利金额(100元起)
     */

    public function actionWithdrawal() {
        if (Yii::$app->request->isAjax) {
            $event = new GradeEvent();
            $this->on("withdrawal_event", [$event, "withdrawal"]);
            $this->trigger("withdrawal_event", $event);
            $this->off("withdrawal_event");
            echo Json::encode(["status" => $this->flag, "msg" => $this->message]);
            exit();
        }
    }

    /* 将返利金额充值进账户 */

    public function actionRebateup() {
        if (Yii::$app->request->isAjax) {
            $event = new GradeEvent();
            $this->on("rebateup_event", [$event, "rebateup"]);
            $this->trigger("rebateup_event", $event);
            $this->off("rebateup_event");
            echo Json::encode(["status" => $this->flag, "msg" => $this->message]);
            exit();
        }
    }

}
